<?php

namespace app\privatearea\controller\mall\v1;

use app\common\cachemodel\privatearea\PrivateAreaMallCompany;
use app\common\cachemodel\privatearea\PrivateAreaMallGoods;
use app\common\cachemodel\privatearea\PrivateAreaMallGoodsAttr;
use app\common\cachemodel\privatearea\PrivateAreaUserAddress;
use app\privatearea\controller\MallAuth;
use think\response\Json;

class Goods extends MallAuth
{
    /**
     * 商品列表
     * @return Json
     * @throws \Exception
     */
    public function index()
    {
        $page = input('page/d', 0);
        $sn = input('sn/s', '');
        if ($page < 1)
        {
            $page = 1;
        }
        if ($sn)
        {
            $gData = $this->decryptWithOpenssl(urldecode($sn));
            $gData = $gData ? json_decode($gData, true) : '';
            if (!$gData)
            {
                return show(config('code.error'), '服务异常，页面加载失败～');
            }
        } else
        {
            $gData = ['end_time' => 1, 'hash' => ''];
        }

        $endTime = $gData['end_time'] ?? 0;
        if ($endTime > 0)
        {   // 上架计时
            $remainingTime = $endTime - $this->request->time();
            if ($remainingTime < 0)
            {   // 已下架
                return show(config('code.error'), '什么都没有～');
            }
        }
        $privateAreaMallGoodsModel = new PrivateAreaMallGoods();
        $pageData = $privateAreaMallGoodsModel->getGoodList($page, 20, []);
        $pageData['data'] = $privateAreaMallGoodsModel->pageDataResert($pageData['data'], $gData);
        return show(config('code.success'), '请求成功', $pageData);
    }

    /**
     * 商品详情
     * @return Json
     */
    public function detail()
    {
        $sn = input('sn/s', '');
        if (empty($sn))
        {
            return show(config('code.error'), '服务异常，商品信息错误～');
        }

        if ($sn === '665876')
        {//微信支付h5审核地址
            $sn = 'Z9qtaCBsCtgUPyLXKPxcgYlZH9kTOcPrqvL44PSZkQrgM0A%2BJIwpsf1WpFYcbIVIQaPugdGcl4AQGTaHE12%2F3A%3D%3D';
        }

        $gData = $this->decryptWithOpenssl(urldecode($sn));
        $gData = $gData ? json_decode($gData, true) : '';
        if (!$gData)
        {
            return show(config('code.error'), '服务异常，商品信息错误～');
        }
        $gid = $gData['gid'] ?? 0;
        $endTime = $gData['end_time'] ?? 0;
        if (!$gid)
        {
            return show(config('code.error'), '服务异常，商品信息错误～');
        }

        $goodDetail = (new PrivateAreaMallGoods())->getGoodDetail($gid, $endTime);
        if ($goodDetail['status'] === 2)
        {
            return show(config('code.error'), '商品已下架');
        }
        $goodDetail['hash'] = $gData['hash'] ?? '';
        return show(config('code.success'), '请求成功', $goodDetail);
    }

    /**
     * 商品介绍信息
     * @return Json
     * @throws \Exception
     */
    public function goodIntro()
    {
        $sid = input('sid/d', 0);
        if (!$sid)
        {
            return show(config('code.error'), '请选择商品规格');
        }
        $privateAreaMallGoodsModel = new PrivateAreaMallGoods();
        $privateAreaMallGoodsAttrModel = new PrivateAreaMallGoodsAttr();
        $goodAttrDetail = $privateAreaMallGoodsAttrModel->getInfoById($sid);
        if (!$goodAttrDetail)
        {
            return show(config('code.error'), '服务异常，商品规格信息错误～');
        }
        $goodDetail = $privateAreaMallGoodsModel->getInfoById($goodAttrDetail['gid']);
        if (!$goodDetail)
        {
            return show(config('code.error'), '服务异常，商品信息错误～');
        }
        $companyData = (new PrivateAreaMallCompany())->getInfoById($goodDetail['cid'], ['pay_type', 'has_quality_check']);
        if (!$companyData)
        {
            return show(config('code.error'), '服务异常，商品信息错误～');
        }
        $defaultAddress = (new PrivateAreaUserAddress())->getDefault($this->user['id']);
        $addressData = null;
        if ($defaultAddress)
        {
            $addressData = [
                'id' => $defaultAddress['id'],
                'phone' => $defaultAddress['phone'],
                'name' => $defaultAddress['name'],
                'address' => sprintf('%s%s%s%s', $defaultAddress['country'], $defaultAddress['province'], $defaultAddress['city'], $defaultAddress['address_desc']),
            ];
        }
        $payModeList = [];
        if ($this->isWexinBrowser() && in_array(2, $companyData['pay_type']))
        {
            $payModeList = [
                [       // 3 => 微信JSAPI支付
                    'trade_type' => 3,
                    'name' => '微信支付',
                    'icon' => '/test/trade_type/trade_type2.png',
                    'is_selected' => 1,
                ],
            ];

            //test 测试在微信里面，是否可以进行支付宝网页支付
            $payModeList[] = [    // 1=>支付宝H5支付
                'trade_type' => 1,
                'name' => '支付宝支付',
                'icon' => '/test/trade_type/trade_type1.png',
                'is_selected' => 0,
            ];
        } else
        {
            if (in_array(2, $companyData['pay_type']))
            {
                $payModeList[] = [ // 2 => 微信H5支付
                    'trade_type' => 2,
                    'name' => '微信支付',
                    'icon' => '/test/trade_type/trade_type2.png',
                    'is_selected' => 1,
                ];
            }
            if (in_array(1, $companyData['pay_type']))
            {
                $payModeList[] = [    // 1=>支付宝H5支付
                    'trade_type' => 1,
                    'name' => '支付宝支付',
                    'icon' => '/test/trade_type/trade_type1.png',
                    'is_selected' => 0,
                ];
            }
        }

        $resData = [
            'address_data' => $addressData,
            'img' => $goodAttrDetail['img'],
            'title' => $goodDetail['name'],
            'spe_name' => $goodAttrDetail['name'],
            'price' => (float)$goodAttrDetail['now_price'],
            'pay_mode_list' => $payModeList,
            'has_quality_check' => $goodDetail['has_quality_check'],
//            'notice' => "1.当前商品为轻定制产品，不支持退款，请您谨慎下单购买。\r\n2.如您未满18岁，请不要购买此商品。\r\n3.商品含天然矿石，不是标准制品，有云雾状、星点状或絮状气泡体，并往往有微裂痕，属正常现象，轻知悉。\r\n4、本次活动默认为不鉴定，若您需第三方鉴定服务，等待周期会较长，请您理解！\r\n以上信息，轻仔细阅读后再购买！",
            'notice' => [
                [
                    'text' => '为了确保您的购物体验更加顺畅和高效，特向您说明以下订单规则：',
                    'type' => 0,
                ],
                [
                    'text' => '1.部分商品为轻定制产品，不支持退款，请您谨慎下单购买。',
                    'type' => 1,
                ],
                [
                    'text' => "2.如您未满18岁，请不要购买此商品。\r\n3.商品含天然矿石，不是标准制品，有云雾状、星点状或絮状气泡体，并往往有微裂痕，属正常现象，轻知悉。\r\n4.三方鉴定的过程可能物流会慢一些，请您理解。
以上信息，轻仔细阅读后再购买！\r\n5. 普通商品下单后 2 小时内，您可以免费取消订单，不会产生任何费用。若超过该时间您若要求拦截订单，需扣除 20 元拦截费用。\r\n\r\n请您在下单时仔细确认商品信息，合理安排订单操作，以免造成不必要的费用损失。我们致力于为您提供优质的服务，感谢您的理解与支持！",
                    'type' => 0
                ],
            ]
        ];
        return show(config('code.success'), '请求成功', $resData);
    }
}
